package com.management.demo.controller.mall;

import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.management.demo.domain.mall.PmsCategory;
import com.management.demo.dto.mall.PmsCategoryDTO;
import com.management.demo.dto.mall.PmsCategorySortDTO;
import com.management.demo.service.PmsCategoryService;
import com.management.demo.vo.mall.PmsCategoryVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "商品分类管理")
@RestController
@RequestMapping("/pms/category")
public class PmsCategoryController {

    @Autowired
    private PmsCategoryService pmsCategoryService;

    @Operation(summary = "分类树形列表")
    @GetMapping("/list/tree")
    public SaResult list() {
        List<PmsCategoryVO> list = pmsCategoryService.listWithTree();
        return SaResult.ok().setData(list);
    }

    @Operation(summary = "新增/修改商品分类信息")
    @PostMapping("createOrEdit")
    public SaResult createOrEdit(@RequestBody PmsCategoryDTO pmsCategoryDTO) {
        PmsCategory pmsCategory = BeanUtil.copyProperties(pmsCategoryDTO, PmsCategory.class);
        boolean result = pmsCategoryService.saveOrUpdate(pmsCategory);
        return SaResult.ok().setData(result);
    }

    @Operation(summary = "交换商品排序信息")
    @PostMapping("updateSort")
    @Transactional
    public SaResult updateSort(@RequestBody PmsCategorySortDTO pmsCategorySortDTO) {
        Integer lastCatId = pmsCategorySortDTO.getLastCatId();
        Integer nextCatId = pmsCategorySortDTO.getNextCatId();
        Integer lastSort = pmsCategorySortDTO.getLastSort();
        Integer nextSort = pmsCategorySortDTO.getNextSort();

        pmsCategoryService.update(new LambdaUpdateWrapper<PmsCategory>().eq(PmsCategory::getCatId, lastCatId).set(PmsCategory::getSort, nextSort));
        pmsCategoryService.update(new LambdaUpdateWrapper<PmsCategory>().eq(PmsCategory::getCatId, nextCatId).set(PmsCategory::getSort, lastSort));

        return SaResult.ok().setData(true);
    }

    @Operation(summary = "根据id获取商品分类信息")
    @GetMapping("getById/{id}")
    public SaResult getById(@Parameter(description = "商品分类id")
                            @PathVariable Integer id) {
        PmsCategory pmsCategory = pmsCategoryService.getById(id);
        PmsCategoryVO result = BeanUtil.copyProperties(pmsCategory, PmsCategoryVO.class);
        Long parentCid = pmsCategory.getParentCid();
        if (parentCid.equals(0L)) {
            result.setParentName("无");
        } else {
            LambdaQueryWrapper<PmsCategory> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(PmsCategory::getCatId, parentCid);
            PmsCategory parentPmsCategory = pmsCategoryService.getOne(queryWrapper);
            result.setParentName(parentPmsCategory.getName());
        }
        return SaResult.ok().setData(result);
    }

    @Operation(summary = "删除商品分类信息")
    @DeleteMapping("remove/{id}")
    public SaResult remove(@Parameter(description = "商品分类id")
                           @PathVariable Integer id) {
        boolean result = pmsCategoryService.removeById(id);
        return SaResult.ok().setData(result);
    }
}
